package petstore;

import petstore.domain.Log;
import petstore.persistence.LogDaoImpl;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import java.io.IOException;

public class logFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //过滤器，拦截对商品页面的请求，用作日志记录
        String categoryId =servletRequest.getParameter("categoryId");//获取从request过来的categoryId
        Log.getInstance().linkedCategoryUpdate(categoryId);//更新条目被访问的次数

        LogDaoImpl logDao = new LogDaoImpl();
        logDao.UpdateLastTime(Log.getInstance(),Log.getInstance().getLastTime());//存到数据库中

        System.out.println("拦截成功");
        servletRequest.getRequestDispatcher("category").forward(servletRequest,servletResponse);//请求转发
    }

    @Override
    public void destroy() {

    }
}
